package com.yichen.rent.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.yichen.rent.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.yichen.rent.dao.ResourceDao;
import com.yichen.rent.pojo.Resource;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class ResourceService {

	@Autowired
	private ResourceDao resourceDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Resource> findAll() {
		return resourceDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Resource> findSearch(Map whereMap, int page, int size) {
		Specification<Resource> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return resourceDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Resource> findSearch(Map whereMap) {
		Specification<Resource> specification = createSpecification(whereMap);
		return resourceDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Resource findById(String id) {
		return resourceDao.findById(id).orElse(null);
	}

	/**
	 * 增加
	 * @param resource
	 */
	public void add(Resource resource) {
		resource.setId( idWorker.nextId()+"" );
		resource.setCreate_time(DateUtil.dateToString(new Date(),"yyyy-MM-dd"));
		resource.setBailor_time(DateUtil.dateToString(new Date(),"yyyy-MM-dd"));
		resource.setStatus("正常");
		resourceDao.save(resource);
	}

	/**
	 * 修改
	 * @param resource
	 */
	public void update(Resource resource) {
		resourceDao.save(resource);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		resourceDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Resource> createSpecification(Map searchMap) {

		return new Specification<Resource>() {

			@Override
			public Predicate toPredicate(Root<Resource> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.equal(root.get("id").as(String.class), searchMap.get("id")));
                }
                // 房源所在省份
                if (searchMap.get("province")!=null && !"".equals(searchMap.get("province")) && !"省".equals(searchMap.get("province"))) {
                	predicateList.add(cb.like(root.get("province").as(String.class), "%"+(String)searchMap.get("province")+"%"));
                }
                // 房源所在城市
                if (searchMap.get("city")!=null && !"".equals(searchMap.get("city")) && !"市".equals(searchMap.get("city"))) {
                	predicateList.add(cb.like(root.get("city").as(String.class), "%"+(String)searchMap.get("city")+"%"));
                }
                // 房源所在县镇
                if (searchMap.get("county")!=null && !"".equals(searchMap.get("county"))&& !"区".equals(searchMap.get("county"))) {
                	predicateList.add(cb.like(root.get("county").as(String.class), "%"+(String)searchMap.get("county")+"%"));
                }
                // 租房方式
                if (searchMap.get("rent_way")!=null && !"".equals(searchMap.get("rent_way"))) {
                	predicateList.add(cb.like(root.get("rent_way").as(String.class), "%"+(String)searchMap.get("rent_way")+"%"));
                }
                // 租金
                if (searchMap.get("rent")!=null && !"".equals(searchMap.get("rent"))) {
                	predicateList.add(cb.like(root.get("rent").as(String.class), "%"+(String)searchMap.get("rent")+"%"));
                }
                // 房屋朝向
                if (searchMap.get("orientation")!=null && !"".equals(searchMap.get("orientation"))) {
                	predicateList.add(cb.like(root.get("orientation").as(String.class), "%"+(String)searchMap.get("orientation")+"%"));
                }
                // 房屋类型
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                	predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
                }
                // 委托人
                if (searchMap.get("bailor")!=null && !"".equals(searchMap.get("bailor"))) {
                	predicateList.add(cb.like(root.get("bailor").as(String.class), "%"+(String)searchMap.get("bailor")+"%"));
                }
                // 房屋面积
                if (searchMap.get("square")!=null && !"".equals(searchMap.get("square"))) {
                	predicateList.add(cb.like(root.get("square").as(String.class), "%"+(String)searchMap.get("square")+"%"));
                }
                // 房屋所在楼层
                if (searchMap.get("floor")!=null && !"".equals(searchMap.get("floor"))) {
                	predicateList.add(cb.like(root.get("floor").as(String.class), "%"+(String)searchMap.get("floor")+"%"));
                }
                // 电梯
                if (searchMap.get("elevator")!=null && !"".equals(searchMap.get("elevator"))) {
                	predicateList.add(cb.like(root.get("elevator").as(String.class), "%"+(String)searchMap.get("elevator")+"%"));
                }
                // 车位
                if (searchMap.get("carport")!=null && !"".equals(searchMap.get("carport"))) {
                	predicateList.add(cb.like(root.get("carport").as(String.class), "%"+(String)searchMap.get("carport")+"%"));
                }
                // 用水
                if (searchMap.get("water")!=null && !"".equals(searchMap.get("water"))) {
                	predicateList.add(cb.like(root.get("water").as(String.class), "%"+(String)searchMap.get("water")+"%"));
                }
                // 用电
                if (searchMap.get("electricity")!=null && !"".equals(searchMap.get("electricity"))) {
                	predicateList.add(cb.like(root.get("electricity").as(String.class), "%"+(String)searchMap.get("electricity")+"%"));
                }
                // 燃气
                if (searchMap.get("gas")!=null && !"".equals(searchMap.get("gas"))) {
                	predicateList.add(cb.like(root.get("gas").as(String.class), "%"+(String)searchMap.get("gas")+"%"));
                }
                // 取暖
                if (searchMap.get("heater")!=null && !"".equals(searchMap.get("heater"))) {
                	predicateList.add(cb.like(root.get("heater").as(String.class), "%"+(String)searchMap.get("heater")+"%"));
                }
                // 租期
                if (searchMap.get("tenancy")!=null && !"".equals(searchMap.get("tenancy"))) {
                	predicateList.add(cb.like(root.get("tenancy").as(String.class), "%"+(String)searchMap.get("tenancy")+"%"));
                }
                // 房屋状态
                if (searchMap.get("status")!=null && !"".equals(searchMap.get("status"))) {
                	predicateList.add(cb.like(root.get("status").as(String.class), "%"+(String)searchMap.get("status")+"%"));
                }
                // 咨询量
                if (searchMap.get("inquiries")!=null && !"".equals(searchMap.get("inquiries"))) {
                	predicateList.add(cb.like(root.get("inquiries").as(String.class), "%"+(String)searchMap.get("inquiries")+"%"));
                }
                // 看房量
                if (searchMap.get("look_for")!=null && !"".equals(searchMap.get("look_for"))) {
                	predicateList.add(cb.like(root.get("look_for").as(String.class), "%"+(String)searchMap.get("look_for")+"%"));
                }
                // 房源标题
                if (searchMap.get("house_title")!=null && !"".equals(searchMap.get("house_title"))) {
                	predicateList.add(cb.like(root.get("house_title").as(String.class), "%"+(String)searchMap.get("house_title")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

	/**
	 * 获取房源总数
	 * @return
	 */
	public Long getCount() {
		return resourceDao.count();
	}

	/**
	 * 获取上月新增房源数
	 * @return
	 */
	public Long getAddCountByMonth() {
		return null;
	}

	public Map<String,String> getCountListByMonth(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int month = calendar.get(Calendar.YEAR);
		Map<String,String> map = new HashMap<>();
		return map;
	}
}
